<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/hexo/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/hexo/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/hexo/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/hexo/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/hexo/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/hexo/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/hexo/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="开发笔记,">










<meta name="description" content="近期做的一个项目，由于需要频繁的上传大文件资源，导致存储空间的需求直线上升，在解决这个问题的过程中，偶然发现了百度网盘有一个开放平台，只需要简单的几步操作，就可以实现本地资源实时上传到百度网盘。当然了，最终我们的项目是没有采用这种方案，因为还是有一些暂时没有解决的问题，就暂时存到了我们自己的附件服务器。而由于之前写好的一些东西已经实现了同步到百度网盘的功能，同时呢因为好好阅读了百度云盘的接口文档，">
<meta name="keywords" content="开发笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="薅bd的羊毛?-1（上传文件到百度网盘）">
<meta property="og:url" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/index.html">
<meta property="og:site_name" content="Tony&#39;s blog">
<meta property="og:description" content="近期做的一个项目，由于需要频繁的上传大文件资源，导致存储空间的需求直线上升，在解决这个问题的过程中，偶然发现了百度网盘有一个开放平台，只需要简单的几步操作，就可以实现本地资源实时上传到百度网盘。当然了，最终我们的项目是没有采用这种方案，因为还是有一些暂时没有解决的问题，就暂时存到了我们自己的附件服务器。而由于之前写好的一些东西已经实现了同步到百度网盘的功能，同时呢因为好好阅读了百度云盘的接口文档，">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/console.png">
<meta property="og:image" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/oauth2.png">
<meta property="og:image" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/precreate.png">
<meta property="og:image" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/precreate2.png">
<meta property="og:image" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/superfile.png">
<meta property="og:image" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/superfile2.png">
<meta property="og:image" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/create.png">
<meta property="og:image" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/final.png">
<meta property="og:image" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/m3u8.png">
<meta property="og:image" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/vlc.png">
<meta property="og:image" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/vlc2.png">
<meta property="og:image" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/dlink.png">
<meta property="og:updated_time" content="2020-06-29T02:05:28.290Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="薅bd的羊毛?-1（上传文件到百度网盘）">
<meta name="twitter:description" content="近期做的一个项目，由于需要频繁的上传大文件资源，导致存储空间的需求直线上升，在解决这个问题的过程中，偶然发现了百度网盘有一个开放平台，只需要简单的几步操作，就可以实现本地资源实时上传到百度网盘。当然了，最终我们的项目是没有采用这种方案，因为还是有一些暂时没有解决的问题，就暂时存到了我们自己的附件服务器。而由于之前写好的一些东西已经实现了同步到百度网盘的功能，同时呢因为好好阅读了百度云盘的接口文档，">
<meta name="twitter:image" content="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/console.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/hexo/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Tony'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tony_df.coding.net/2020/05/21/薅bd的羊毛/">





  <title>薅bd的羊毛?-1（上传文件到百度网盘） | Tony's blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/hexo/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Tony's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">学着码代码,学着码人生;</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/hexo/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/hexo/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br>
            
            Tags
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/hexo/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br>
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tony_df.coding.net/hexo/2020/05/21/薅bd的羊毛/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tony">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/hexo/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tony's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">薅bd的羊毛?-1（上传文件到百度网盘）</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-05-21T19:08:15+08:00">
                2020-05-21
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>近期做的一个项目，由于需要频繁的上传大文件资源，导致存储空间的需求直线上升，在解决这个问题的过程中，偶然发现了百度网盘有一个开放平台，只需要简单的几步操作，就可以实现本地资源实时上传到百度网盘。<br>当然了，最终我们的项目是没有采用这种方案，因为还是有一些暂时没有解决的问题，就暂时存到了我们自己的附件服务器。<br>而由于之前写好的一些东西已经实现了同步到百度网盘的功能，同时呢因为好好阅读了百度云盘的接口文档，又联想到前阵子出事儿的“pandownload”事件，这些事情综合起来，我觉得非常巧，也非常有意思，就想拿出来说说。<br>百度网盘开放平台的地址：<a href="https://pan.baidu.com/union#/" target="_blank" rel="noopener">https://pan.baidu.com/union#/</a><br>根据平台的介绍，百度网盘开放了很多能力，包括图片处理，音视频，上传文件，下载文件，附件能力，nas能力，文件提取，设备管理等等。<br>我们在解决文件存储问题的过程，主要是用到了上传文件和音视频的接口。<br>来看下整个的接入流程<br>首先，百度网盘开放平台，并不是像百度云或者其他云服务一样，针对所有用户都开放的，需要提交使用申请，通过之后方可接入相关能力。<br>申请地址如下:<a href="https://pan.baidu.com/union/apply" target="_blank" rel="noopener">https://pan.baidu.com/union/apply</a>，这个没什么好说的，技术是人家的，想拿来用就得按人家的规则来。一般提交了申请，1，2天结果就下来了。<br>申请下来之后，到开放平台的控制台<a href="http://developer.baidu.com/console#app/project" target="_blank" rel="noopener">http://developer.baidu.com/console#app/project</a>创建一个工程（和百度云控制台的工程是互通的）<br><img src="console.png" alt="console"><br>创建完成后，会得到一个应用的相关信息，同时根据平台的建议，需要设定一个可以访问到的回调页<br><img src="oauth2.png" alt="oauth2"><br>当然不设定也是可以的，下面我会提到，但如果是生产环境使用的话，最好是设定这样一个回调页。<br>这一部分平台的相关帮助内容在这个页面<a href="http://developer.baidu.com/wiki/index.php?title=docs/oauth/redirect" target="_blank" rel="noopener">http://developer.baidu.com/wiki/index.php?title=docs/oauth/redirect</a><br>设定完成后，我们可以访问一个特定的链接，用到工程项目中的apikey和secretkey以及回调链接当作参数，具体如下<br><a href="http://openapi.baidu.com/oauth/2.0/authorize" target="_blank" rel="noopener">http://openapi.baidu.com/oauth/2.0/authorize</a>?<br>    response_type=code&amp;<br>    client_id=YOUR_CLIENT_ID&amp;<br>    redirect_uri=YOUR_REGISTERED_REDIRECT_URI&amp;<br>    scope=email&amp;<br>    display=popup<br>这一步完成后，会跳转到我们设定的那个回调页，同时会多一个code参数，拿到这个code就可以取换取token了（一个标准的Oauth2.0操作）<br>前面提到，如果不设定回调页也是可以的，在对应的url参数上填写“obb”固定值，访问拼接好的连接后，会跳转到一个百度提供的回调页，也可以得到code，但这不是建议的方式。<br>这一步，主要涉及到跳转，传参，取值等操作，取到code之后，基本这一步以后就不会再用到了，因为换取到access_token的同时，还会得到一个refresh_token，其中access_token的有效期是一个月，而refresh_token的有效期确实10年！也就是说，成功拿到access_token之后，有10年的使用期~~<br>至此，前戏结束，拿到令牌以后，后续就容易多了。<br>接下来，要把大象装冰箱，总共分三步。我们要把我们的大文件传到百度云盘，也分三步，接口文档为<a href="https://pan.baidu.com/union/document/upload" target="_blank" rel="noopener">https://pan.baidu.com/union/document/upload</a>。<br>文档写的听明白了，但我觉得如果你之前很少用过云服务，看起文档来十有八九都是越看越懵逼的，所以我这就提供个例子。<br>1.预上传<br><img src="precreate.png" alt="precreate"><br><img src="precreate2.png" alt="precreate2"><br>这里我就想说一下那个block_list这个参数，这个值是文件的md5值，在第一步预上传的时候，可以通过程序来获取该值。<br>实现的接口代码就非常简单了，也简单贴一下</p>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 预上传，参数模型</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">BdNasPrecreate</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 上传后使用的文件绝对路径</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> path &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件或目录的大小，单位是字节(B)</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> size &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 是否目录，0 文件、1 目录</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> isdir &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125; = <span class="string">"0"</span>;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 固定值1</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> autoinit &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125; = <span class="number">1</span>;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件命名策略，默认0</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span>0 为不重命名，返回冲突</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span>1 为只要path冲突即重命名</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span>2 为path冲突且block_list不同才重命名</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span>3 为覆盖</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> rtype &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125; = <span class="number">3</span>;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件各分片MD5的json串(调用的时候，给一个md5格式的默认值，可以把文件名的md5值传入)</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> List&lt;<span class="keyword">string</span>&gt; block_list &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 预上传返回参数</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">BdNasPrecreateReponse</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件的绝对路径</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> path &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 上传id</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> uploadid &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 返回类型，1 文件在云端不存在、2 文件在云端已存在</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> return_type &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 需要上传的分片序号，索引从0开始</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> List&lt;<span class="keyword">int</span>&gt; block_list &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 错误码，0-正常</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> errno &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 请求id</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">long</span> request_id &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 第一步，预请求</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="precreate"&gt;</span><span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line">[<span class="meta">HttpPost</span>]</span><br><span class="line"><span class="function"><span class="keyword">public</span> Response <span class="title">UploadFile1</span>(<span class="params">[FromBody]BdNasPrecreate precreate</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Response resp = <span class="keyword">new</span> Response();</span><br><span class="line">    <span class="keyword">try</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">var</span> token = GetAccessToken();</span><br><span class="line">        <span class="keyword">string</span> url = <span class="string">$"http://pan.baidu.com/rest/2.0/xpan/file?method=precreate&amp;access_token=<span class="subst">&#123;token.access_token&#125;</span>"</span>;        </span><br><span class="line">        <span class="keyword">string</span> ret = Common.RequestHelper.HttpPost(url, JsonHelper.JsonSerialize(precreate), <span class="literal">null</span>, <span class="string">"application/x-www-form-urlencoded"</span>);</span><br><span class="line">        <span class="keyword">if</span> (ret.Contains(<span class="string">"uploadid"</span>))</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">var</span> first = JsonHelper.JsonDeserialize&lt;BdNasPrecreateReponse&gt;(ret);</span><br><span class="line">            resp.code = <span class="number">1</span>;</span><br><span class="line">            resp.message = <span class="string">"success"</span>;</span><br><span class="line">            resp.data = <span class="keyword">new</span> &#123; first &#125;;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">catch</span> (Exception ex)</span><br><span class="line">    &#123;</span><br><span class="line">        resp.code = <span class="number">-1</span>;</span><br><span class="line">        resp.message = <span class="string">$"error:预请求失败,<span class="subst">&#123;ex.Message&#125;</span>,<span class="subst">&#123;ex.StackTrace&#125;</span>"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> resp;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>第一步里的返回参数里，最重要的就是uploadid了，这是第二步里要用到的一个参数。<br>2.分片上传<br>第二步需要注意的就是，除了文件需要放到请求body里，其他参数都需要放到url里，看下postman的实例<br><img src="superfile.png" alt="superfile"><br><img src="superfile2.png" alt="superfile2"><br>对应的接口代码</p>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 分片上传,只有file需要置于form表单中，其余参数需要置于querystring中</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span>1、普通用户单个分片大小固定为4MB</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span>2、普通会员用户单个分片大小上限为16MB</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span>3、超级会员用户单个分片大小上限为32MB</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">BdNasMultipart</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 固定值 upload</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> method &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125; = <span class="string">"upload"</span>;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 固定值 tmpfile</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> type &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125; = <span class="string">"tmpfile"</span>;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 上传后使用的文件绝对路径</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> path &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> precreate接口下发的uploadid</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> uploadid &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件分片的位置序号，参考precreate接口返回的block_list</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> partseq &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件是否在服务器本地存一份</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> save_local &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125; = <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 分片上传返回参数</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">BdNasMultipartResponse</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件md5值</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> md5 &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 请求值</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> request_id &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 第二步，分片上传</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="mulitepart"&gt;</span><span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line">[<span class="meta">HttpPost</span>]</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">async</span> Task&lt;Response&gt; <span class="title">UploadFile2</span>(<span class="params">[FromQuery]BdNasMultipart mulitepart</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Response resp = <span class="keyword">new</span> Response();</span><br><span class="line">    <span class="keyword">try</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">var</span> files = Request.Form.Files;</span><br><span class="line">        <span class="keyword">if</span> (files.Count() == <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            resp.code = <span class="number">-1</span>;</span><br><span class="line">            resp.message = <span class="string">"没有检测到上传文件"</span>;</span><br><span class="line">            <span class="keyword">return</span> resp;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">string</span> path = <span class="keyword">await</span> UploadFile(files[<span class="number">0</span>]);</span><br><span class="line">        List&lt;<span class="keyword">string</span>&gt; filelist = <span class="keyword">new</span> List&lt;<span class="keyword">string</span>&gt;();</span><br><span class="line">        filelist.Add(path);</span><br><span class="line">        <span class="keyword">var</span> token = GetAccessToken();</span><br><span class="line">        <span class="keyword">string</span> url = <span class="string">$"https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?method=<span class="subst">&#123;mulitepart.method&#125;</span>&amp;type=<span class="subst">&#123;mulitepart.type&#125;</span>&amp;path=<span class="subst">&#123;mulitepart.path&#125;</span>&amp;uploadid=<span class="subst">&#123;mulitepart.uploadid&#125;</span>&amp;partseq=<span class="subst">&#123;mulitepart.partseq&#125;</span>&amp;access_token=<span class="subst">&#123;token.access_token&#125;</span>"</span>;</span><br><span class="line">        <span class="keyword">string</span> ret =  RequestHelper.HttpPostAsync(url, <span class="literal">null</span>, filelist);</span><br><span class="line">        <span class="keyword">if</span> (ret.Contains(<span class="string">"md5"</span>))</span><br><span class="line">        &#123;</span><br><span class="line">            resp.code = <span class="number">1</span>;</span><br><span class="line">            resp.message = ret;</span><br><span class="line">            <span class="keyword">var</span> second = JsonHelper.JsonDeserialize&lt;BdNasMultipartResponse&gt;(ret);</span><br><span class="line">            resp.data = <span class="keyword">new</span> &#123; second &#125;;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">catch</span> (Exception ex)</span><br><span class="line">    &#123;</span><br><span class="line">        resp.code = <span class="number">-1</span>;</span><br><span class="line">        resp.message = <span class="string">$"error:分片上传失败,<span class="subst">&#123;ex.Message&#125;</span>,<span class="subst">&#123;ex.StackTrace&#125;</span>"</span>;</span><br><span class="line">        NLogUtil.fileLogger.Error(<span class="string">$"error:分片上传失败,<span class="subst">&#123;ex.Message&#125;</span>,<span class="subst">&#123;ex.StackTrace&#125;</span>"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> resp;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>3.创建文件<br><img src="create.png" alt="create"><br>对应的接口代码</p>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">BdNasCreate</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 上传后使用的文件绝对路径</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> path &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件或目录的大小，必须要和文件真实大小保持一致</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> size &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 是否目录，0 文件、1 目录</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> isdir &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125; = <span class="number">0</span>;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件命名策略，默认1</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 0 为不重命名，返回冲突</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 1 为只要path冲突即重命名</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 2 为path冲突且block_list不同才重命名</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 3 为覆盖</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> rtype &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125; = <span class="number">3</span>;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件各分片MD5的json串</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> MD5对应superfile2返回的md5，且要按照序号顺序排列</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> List&lt;<span class="keyword">string</span>&gt; block_list &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">BdNasCreateResponse</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件在云端的唯一标识ID</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">long</span> fs_id &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件的MD5，只有提交文件时才返回，提交目录时没有该值</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> md5 &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件名</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> server_filename &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 分类类型, 1 视频 2 音频 3 图片 4 文档 5 应用 6 其他 7 种子</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> category &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 上传后使用的文件绝对路径</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">string</span> path &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件大小，单位B</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">long</span> size &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件创建时间</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">long</span> ctime &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 文件修改时间</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">long</span> mtime &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 是否目录，0 文件、1 目录</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> isdir &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> 第三步，创建文件</span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="create"&gt;</span><span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line">[<span class="meta">HttpPost</span>]</span><br><span class="line"><span class="function"><span class="keyword">public</span> Response <span class="title">UploadFile3</span>(<span class="params">[FromBody] BdNasCreate create</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Response resp = <span class="keyword">new</span> Response();</span><br><span class="line">    <span class="keyword">try</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">var</span> token = GetAccessToken();</span><br><span class="line">        <span class="keyword">string</span> url = <span class="string">$"https://pan.baidu.com/rest/2.0/xpan/file?method=create&amp;access_token=<span class="subst">&#123;token.access_token&#125;</span>"</span>;</span><br><span class="line">        <span class="keyword">string</span> ret = Common.RequestHelper.HttpPost(url, JsonHelper.JsonSerialize(create), <span class="literal">null</span>, <span class="string">"application/x-www-form-urlencoded"</span>);</span><br><span class="line">        <span class="keyword">if</span> (ret.Contains(<span class="string">"fs_id"</span>))</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">var</span> third = JsonHelper.JsonDeserialize&lt;BdNasCreateResponse&gt;(ret);</span><br><span class="line">            resp.code = <span class="number">1</span>;</span><br><span class="line">            resp.message = <span class="string">"success"</span>;</span><br><span class="line">            resp.data = <span class="keyword">new</span> &#123; third &#125;;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">catch</span>(Exception ex)</span><br><span class="line">    &#123;</span><br><span class="line">        resp.code = <span class="number">-1</span>;</span><br><span class="line">        resp.message = <span class="string">$"error:创建文件,<span class="subst">&#123;ex.Message&#125;</span>,<span class="subst">&#123;ex.StackTrace&#125;</span>"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> resp;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>三步操作完成后，打开百度网盘，我们就看到我们刚上传的文件啦<br><img src="final.png" alt="final"><br>这里我分享的是上传文件每个步骤拆开后的单独操作和对应开发的接口，实际应用的时候，我们可能需要把三步操作合并成一步，其中可能会涉及到一些网络延时等问题，需要按实际的业务需求酌情处理。m<br>一般此类操作都是在后台或者单独的服务中另起线程来运行。<br>至此，上传的整个流程就完成了，剩下的就是怎么更好的融入到业务系统了。<br>再简单介绍下获取视频流的接口<br>其实百度虽然开放了视频能力，而接口文档写的实在有限。但是话又说回来了，人家都把能力放开给你用了，而且这个能力比多大多数个人和团队自己处理的要好的多得多得多，还要什么自行车！<br>我个人觉得，要彻底理解平台开放视频能力，需要有一些预备知识，比如hls协议，hls和rtmp协议的区别，m3u8是什么类型的文件，文件中的各个字段都代表什么意思等等。<br>这个我就不多说了，我是觉得视频处理这块，每个环节单独拎出来都是很大的一块，我也在学习中，就直接看一个例子吧<br><img src="m3u8.png" alt="m3u8"><br>这个其实在接口里看是没有实际意义的，但这样可以直接看到百度返回来的m3u8格式的文件内容，帮助我们分析，实际使用的时候，该接口需要直接反馈到终端播放器即可完成视频输出。<br>比如我这里用VLC播放器来演示一下<br><img src="vlc.png" alt="vlc1"><br><img src="vlc2.png" alt="vlc2"><br>至此，就完成了视频播放的集成，这里我想说的是，我们在项目中集成该功能时，因为是web系统，在浏览器中会出现跨域的错误，暂时没办法解决，而如果你的终端时app或者桌面程序，那就没有这个问题了。最后，我想再回到文章开头提到的“pandownload”的事件，其实吧，一开始听到这个新闻，我还觉得这个作者挺牛逼的，还查了一下，说这个软件的工作原理，用到什么aria2等工具来加速下载。<br>但是如果你读过开放平台上的文档，就会发现，这个软件在怎么牛逼，但凡事要下载，你得解析出实际的下载地址来吧，而这个实际的下载地址，开放平台提供的接口里是可以获取到的，叫做dlink.<br>如下图<br><img src="dlink.png" alt="dlink"><br>我们可以很容易获取到该值，而拿到该文件地址之后，就可以尽情下载了，所以反向来推理下“pandownload”的原理，或者我们来根据平台提供的能力来模拟一个类似的逻辑。<br>1.获得平台能力-&gt;2.获取文件-&gt;3.将文件转存到个人云盘-&gt;4.获取下载链接，其中第三步我自己没试过，其他几步都没问题，所以呢，pandownload如果是按照这个思路来实现的话，那我觉得这个作者可能是把自己当成燕子李三了，以为网盘的用户都是贫困户，自己知道了怎么薅百度的羊毛，还免费让别人也来他这薅，这就是有点内味了吧。<br>最后，再总结下，我个人觉得百度其实挺厚道的，虽然最后没有继承它的视频能力，但上传，下载等功能还是非常实用的，而且账号如果开通了vip，接口使用起来也会更顺畅，用非常低的成本，获得了原本需要几倍成本才有的存储能力，还是非常厚道的。不过还是要提醒下，百度的文档里说的也很清楚，如果要继承其接口能力，在产品发布的时候，最好是给人家发个邮件告知一下，别薅high了，薅过头了，回头人家反手就把你告上公堂，你也无话可说啊，“pandownload”的事件就是个典型的例子。<br>好了，接下来几篇，还是聊项目中用到的东西，对效率提升较明显的东西，比如语法上的泛型委托，工具上的消息队列中间件等等。<br>距离上篇博客隔了一个多月了，其实早想写了，但真心是没时间~</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/hexo/tags/开发笔记/" rel="tag"># 开发笔记</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/hexo/2020/04/29/从一次实践出发聊两个和数据库有关的点/" rel="next" title="从一次实践出发聊两个和数据库有关的点">
                <i class="fa fa-chevron-left"></i> 从一次实践出发聊两个和数据库有关的点
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/hexo/2020/06/21/薅bd的羊毛-2/" rel="prev" title="薅bd的羊毛?-2（从百度网盘下载文件）">
                薅bd的羊毛?-2（从百度网盘下载文件） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- Go to www.addthis.com/dashboard to customize your tools -->
<div class="addthis_inline_share_toolbox">
  <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5bbb2338b87602f0" async="async"></script>
</div>

      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zOTA1My8xNTU4MA"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Tony</p>
              <p class="site-description motion-element" itemprop="description">学习总结</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/hexo/archives/">
              
                  <span class="site-state-item-count">58</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/hexo/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Tony</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/hexo/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/hexo/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/hexo/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/hexo/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/hexo/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/hexo/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/hexo/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/hexo/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/hexo/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/hexo/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/hexo/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  

  

  

  

</body>
</html>
